#include "ULGfx_Balls.h"

//=============ULG_Ball=============
ULG_Ball::ULG_Ball(const char *ballImageFile, VSDL_RGB colorkey)
{
	LoadImageFile(ballImageFile, colorkey);
}

int ULG_Ball::LoadImageFile(const char *ballImageFile, VSDL_RGB colorkey)
{
	if (ballImageFile == NULL){
		return -1;
	}

	if (ballImage.LoadImage(ballImageFile, colorkey) < 0){
		return -1;
	}
	currentAlpha = 255;
       	incAlpha = false;
	ballWidth = ballImage.GetImageWidth();
	ballHeight = ballImage.GetImageHeight();	

	return 0;
}

#define BALL_IMAGE_FIXED_GAP	2

int ULG_Ball::Show(SDL_Surface *destSurf, VSDL_Point leftTopXY)
{
	VSDL_Point numberStringXY;

	if (incAlpha){
		currentAlpha += 3;
		
		if (currentAlpha > 255){
			currentAlpha = 255;
			incAlpha = false;
		}
	}else{
		currentAlpha -= 3;
		
		if (currentAlpha < 205){
			currentAlpha = 205;
			incAlpha = true;
		}
	}

	ballImage.SetAlpha(currentAlpha);
	numberStringXY.x = leftTopXY.x + (ballWidth >> 1) - (fontWidth >> 1) - BALL_IMAGE_FIXED_GAP;
	numberStringXY.y = leftTopXY.y + (ballWidth >> 1) - (fontHeight >> 1) - BALL_IMAGE_FIXED_GAP;
	ballImage.Blit(destSurf, leftTopXY);
	msg.Blit(destSurf, numberStringXY);

	return 0;
}

void ULG_Ball::SetNumber(int number)
{
	char stringNumber[4] = {0};

	sprintf(stringNumber, "%02d", number);

	msg.SetText(stringNumber);
}

void ULG_Ball::SetFont(VSDL_Font *font)
{
	if (font != NULL){
		char buf[12];
		sprintf(buf, "99");
		msg.SetFont(font);
		msg.GetTextSize(&fontWidth, &fontHeight);
	}
}

//================ULG_Balls===============
ULG_Balls::ULG_Balls():initFlags(0)
{
}

ULG_Balls::ULG_Balls(const char *redBallImageFile, VSDL_RGB colorkeyRedBall, const char *blueBallImageFile, VSDL_RGB colorkeyBlueBall)
{
	LoadImageFiles(redBallImageFile, colorkeyRedBall, blueBallImageFile, colorkeyBlueBall);
}


ULG_Balls::ULG_Balls(const char *redBallImageFile, VSDL_RGB colorkeyRedBall, const char *blueBallImageFile, VSDL_RGB colorkeyBlueBall, VSDL_Point startXY)

{
	LoadImageFiles(redBallImageFile, colorkeyRedBall, blueBallImageFile, colorkeyBlueBall);
	this->startXY = startXY;
}

int ULG_Balls::LoadImageFiles(const char *redBallImageFile, VSDL_RGB colorkeyRedBall, const char *blueBallImageFile, VSDL_RGB colorkeyBlueBall)
{
	if (redBallImageFile == NULL || blueBallImageFile == NULL){
		return -1;
	}

	initFlags = 0;

	if (redBall.LoadImageFile(redBallImageFile, colorkeyRedBall) == 0){
		initFlags |= ULG_BALLS_F_RED_BALL_IMAGE_SET;
	}

	if (blueBall.LoadImageFile(blueBallImageFile, colorkeyBlueBall) == 0){
		initFlags |= ULG_BALLS_F_BLUE_BALL_IMAGE_SET;
	}

	return 0;
}

void ULG_Balls::SetFirstBallXY(VSDL_Point point)
{
	startXY = point;
}

void ULG_Balls::SetBallNumberFont(VSDL_Font *font)
{
	if (font != NULL){
		redBall.SetFont(font);
		blueBall.SetFont(font);
	}
}

int ULG_Balls::Show(SDL_Surface *destSurf)
{
	VSDL_Point ballXY;
	VSDL_Point msgXY;
	int ballWidth;
	int ballHeight;
	int msgWidth;
	int msgHeight;
	char stringNumber[8];

	if (destSurf == NULL || initFlags == 0){
		return -1;
	}

	if (! (initFlags & ULG_BALLS_F_FONT_COLOR_SET)){
		redBall.msg.SetFgColor(255, 255, 255);
		blueBall.msg.SetFgColor(255, 255, 255);
	}




	ballXY = startXY;
	//Deal with red ones
	if (initFlags & ULG_BALLS_F_RED_BALL_IMAGE_SET){
		ballWidth = redBall.ballWidth;
		ballHeight = redBall.ballHeight;


		for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
			ballXY.x = startXY.x + i * ballWidth;

			//Show ball image
			redBall.SetNumber(reds[i]);
			redBall.Show(destSurf, ballXY);	
		}	

		//Update ballXY for blue use
		ballXY.x += ballWidth;
	}	

	//Deal with blue one
	if (initFlags & ULG_BALLS_F_BLUE_BALL_IMAGE_SET && outputBlue){

		blueBall.SetNumber(blue);
		blueBall.Show(destSurf, ballXY);
		blueBall.msg.Blit(destSurf, msgXY);
	}

	return 0;
}

int ULG_Balls::GetRedBallImageWidth()
{
	return redBall.ballImage.GetImageWidth();
}

int ULG_Balls::GetRedBallImageHeight()
{
	return redBall.ballImage.GetImageHeight();
}

int ULG_Balls::GetBlueBallImageWidth()
{
	return blueBall.ballImage.GetImageWidth();
}

int ULG_Balls::GetBlueBallImageHeight()
{
	return blueBall.ballImage.GetImageHeight();
}

ULG_Balls &ULG_Balls::operator=(const ULC_Balls &ulcBalls)
{
	reds = ulcBalls.reds;
	blue = ulcBalls.blue;
	outputBlue = ulcBalls.outputBlue;

	return *this;	
}

ULG_Balls &ULG_Balls::operator=(const ULG_Balls &ulgBalls)
{

	*this = static_cast<ULC_Balls>(ulgBalls);

	return *this;
}
